home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d17 / labels.arc / LABELS.C < prev    next >
C/C++ Source or Header  |  1988-05-12  |  5KB  |  211 lines

  1. /* LABELS - A Program to Produce Mailing Labels from a Text File */
  2. /* Version 1.0 by Richard Conn */
  3.  
  4. /* Usage: labels [-L#][-C#][-B#] filename.typ */
  5.  
  6. /* Instructions:
  7.    The mailing labels file consists of a sequence of lines, where
  8.    lines beginning in column 1 (non-space - no blank or tab in
  9.    column 1) indicate a new label.  This program assumes a standard
  10.    mailing label of 8 lines by 40 columns with a blank line between
  11.    labels.  Lines beginning with a semicolon (;) are interpreted as
  12.    comments and are not displayed in the output.  Lines beginning with
  13.    a double quote (") are interpreted literally and are output
  14.    without centering.  Blank lines are ignored.
  15.  
  16.    The options can be used to change the default values of the label
  17.    parameters.  The -L# sets the number of lines on the label,
  18.    -C# sets the number of columns, and -B# sets the number of blank
  19.    lines between labels.
  20.  
  21.    Diagnostics are recorded in the log file ERRLOG (defined below).
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26.  
  27. #define MAXLINES 20
  28. #define MAXCOLS  80
  29. #define NLINES 8
  30. #define NCOLS 40
  31. #define NBLANKS 1
  32. #define LINELEN 200
  33. #define ERRLOG "labels.log"
  34. #define COMMENT ';'
  35. #define QUOTE '"'
  36.  
  37. char line[MAXLINES][MAXCOLS+2];
  38. FILE *fdo;
  39. int emptylabel;
  40. int labelcnt;
  41. int columns = NCOLS;
  42. int lines = NLINES;
  43. int blanks = NBLANKS;
  44. int opterr = 0;
  45.  
  46. main (argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50.     char inline[LINELEN];
  51.         char *curchar;
  52.     int curline;
  53.         FILE *fd, *fopen();
  54.     int firsttime = 1;
  55.     int msgprinted = 0;
  56.     int i;
  57.     int fnindex = 0;
  58.  
  59.     labelcnt = 0;
  60.     if (argc < 2) {
  61.         help (argv[0]);
  62.         exit (0);
  63.     }
  64.     for (i=1; i<argc; i++) {
  65.         switch (*argv[i]) {
  66.             case '-' : option (&argv[i][1]);
  67.                    break;
  68.             default  : fnindex = i;
  69.                    break;
  70.         }
  71.     }
  72.     if ((fnindex == 0) || (opterr)) {
  73.         help (argv[0]);
  74.         if (opterr > 1) {
  75.             switch (opterr) {
  76.                 case 2 : printf (" Max Columns Exceeded\n");
  77.                      break;
  78.                 case 3 : printf (" Max Lines Exceeded\n");
  79.                      break;
  80.                 default: break;
  81.             }
  82.         }
  83.         exit (0);
  84.     }
  85.     if ((fd = fopen (argv[fnindex], "r")) == NULL) {
  86.         printf (" Cannot open %s\n", argv[fnindex]);
  87.         exit (0);
  88.     }
  89.     if ((fdo = fopen (ERRLOG, "w")) == NULL) {
  90.         printf (" Cannot create log file %s\n", ERRLOG);
  91.         exit(0);
  92.     }
  93.     fprintf (fdo, " Label Parameters:");
  94.     fprintf (fdo, "  Lines = %d, Columns = %d, Blanks = %d\n",
  95.         lines, columns, blanks);
  96.     while (fgets (inline, LINELEN, fd) != NULL) {
  97.         if ((inline[0] != COMMENT) && (inline[0] != '\n')) {
  98.             if (inline[0] > ' ') {
  99.                 if (firsttime) firsttime--;
  100.                 else printlabel();
  101.                 newlabel();
  102.                 msgprinted = 0;
  103.                 curline = -1;
  104.             }
  105.             curline++;
  106.             curchar = inline;
  107.             while ((*curchar == ' ') || (*curchar == '\t'))
  108.                 curchar++;
  109.             if (curline >= lines) {
  110.                 if (!msgprinted) {
  111.                 fprintf (fdo, " Number of Lines Exceeded\n");
  112.                 for (i=0; i<lines; i++)
  113.                     fprintf (fdo, "   %s", line[i]);
  114.                 msgprinted = 1;
  115.                 }
  116.             } else
  117.                 setline (curline, curchar);
  118.             }
  119.         }
  120.     fclose (fd);
  121.     if (!emptylabel) printlabel();
  122.     fprintf (fdo, " %d Labels Printed\n", labelcnt);
  123.     fclose (fdo);
  124. }
  125.  
  126. /* Print help message */
  127. help (str)
  128. char *str;
  129. {
  130.     printf (" Syntax: %s [-L#] [-C#] [-B#] filename.typ\n",
  131.         str);
  132.     printf ("   Where:\n");
  133.     printf ("     -L# is the number of text lines on the label\n");
  134.     printf ("     -C# is the number of columns on the label\n");
  135.     printf ("     -B# is the number of blank lines between labels\n");
  136.     printf ("   Default values are L=%d, C=%d, B=%d\n",
  137.         NLINES, NCOLS, NBLANKS);
  138.     printf ("   Maximum values are L=%d, C=%d\n", MAXLINES, MAXCOLS);
  139. }
  140.  
  141. /* Process command-line options */
  142. option (str)
  143. char *str;
  144. {
  145.     switch (*str) {
  146.         case 'b' :
  147.         case 'B' : blanks = atoi (++str);
  148.                break;
  149.         case 'c' :
  150.         case 'C' : columns = atoi (++str);
  151.                if (columns > MAXCOLS) opterr = 2;
  152.                break;
  153.         case 'l' :
  154.         case 'L' : lines = atoi (++str);
  155.                if (lines > MAXLINES) opterr = 3;
  156.                break;
  157.         default  : opterr = 1;
  158.                break;
  159.     }
  160. }
  161.  
  162. /* Store the next line into the label image */
  163. setline (number, inline)
  164. int number;
  165. char *inline;
  166. {
  167.     int spacein;
  168.         int i;
  169.  
  170.     if (inline[0] == QUOTE) {
  171.         strcpy (line[number], &inline[1]);
  172.     } else {
  173.         if (strlen(inline) > columns) {
  174.             spacein = 0;
  175.             fprintf (fdo, " Truncation of:\n    %s", inline);
  176.             inline[columns] = '\n';
  177.             inline[columns+1] = '\0';
  178.         } else
  179.             spacein = (columns - strlen(inline) - 1) / 2;
  180.         for (i=0; i<spacein; i++) line[number][i] = ' ';
  181.         strcpy (&line[number][spacein], inline);
  182.     }
  183.     emptylabel = 0;
  184. }
  185.  
  186. /* Initialize the label */
  187. newlabel ()
  188. {
  189.     int i;
  190.  
  191.     for (i=0; i<lines; i++) {
  192.         line[i][0] = '\n';
  193.         line[i][1] = '\0';
  194.     }
  195.     emptylabel = 1;
  196. }
  197.  
  198. /* Print the label */
  199. printlabel ()
  200. {
  201.     int i;
  202.  
  203.     if (emptylabel)
  204.         fprintf (fdo, " Attempt to print empty label\n");
  205.     else {
  206.         for (i=0; i<lines; i++) printf ("%s", line[i]);
  207.         for (i=0; i<blanks; i++) printf ("\n");
  208.         labelcnt++;
  209.     }
  210. }
  211.